package com.hengtiansoft.lfy.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hengtiansoft.lfy.R;
import com.hengtiansoft.lfy.constants.Constants;
import com.hengtiansoft.lfy.utils.Utils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.HashMap;

public class Change_SceneTypeAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private ArrayList<HashMap<String, Object>> mList;
    SceneTypeAdapterListener mListener;
    private  int width=0;
   // private int imgWigth=0;
  //  private int imgHeight=0;
    private int categroyId=2;
    /** 根据手机的分辨率从 dp 的单位 转成为 px(像素)
    */
    public  int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public void setAdapterListener(SceneTypeAdapterListener listener) {
        this.mListener = listener;
    }

    public Change_SceneTypeAdapter(Context context,
                                   ArrayList<HashMap<String, Object>> list, Integer categroyId) {
        mContext = context;
        this.mList = list;
        mInflater = LayoutInflater.from(mContext);
        DisplayMetrics metric = new DisplayMetrics();
        ((Activity)mContext).getWindowManager().getDefaultDisplay().getMetrics(metric);
        width= metric.widthPixels;
       // imgWigth=width-dip2px(mContext,15);
      //  imgHeight=(int)((float)imgWigth/51*30);
        this.categroyId=categroyId;
    }

    public final class ViewHolder {
        TextView scene_name_id;
        ImageView scene_img_id;
        TextView scene_desc_id;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public HashMap<String, Object> getItem(int position) {
        return mList.get(position);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.change_fragment_conversation_scene_listview_item, null);
            holder.scene_name_id = (TextView) convertView.findViewById(R.id.scene_name_id);
            holder.scene_img_id = (ImageView) convertView.findViewById(R.id.scene_img_id);
           // holder.scene_img_id.setLayoutParams(new RelativeLayout.LayoutParams(imgWigth,imgHeight));
           // holder.down_img_id = (ImageView) convertView.findViewById(R.id.down_img_id);
           // holder.scene_detail_id = (TextView) convertView.findViewById(R.id.scene_detail_id);
            holder.scene_desc_id= (TextView) convertView.findViewById(R.id.scene_desc_id);
           // holder.jindu_layout_id= (LinearLayout) convertView.findViewById(R.id.jindu_layout_id);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        HashMap<String, Object> item = getItem(position);
        if(item.get("child_img_url")!=null){
                //加载图片 placeholder
                Picasso.with(mContext).load(Constants.imageURL + item.get("child_img_url").toString()).placeholder(R.drawable.icon_eat).fit()
                        .into(holder.scene_img_id);

        }
           // holder.scene_name_id.setVisibility(View.GONE);
            holder.scene_name_id.setText(item.get("text").toString());
        if(item.get("downLoadCount")==null||"0".equals(item.get("downLoadCount"))){
            holder.scene_desc_id.setText("");
        }else{
            holder.scene_desc_id.setText(item.get("downLoadCount").toString());//	|	" + item.get("downLoadCount").toString() + "次下载"
        }


        convertView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                if (mListener != null)
                    mListener.onItemClick(position);
            }
        });
        return convertView;
    }

    public interface SceneTypeAdapterListener {

        public void onItemClick(int position);

    }
}
